<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

include_once LIBS_PATH.'libraries/jpush/autoload.php';

use JPush\Model as M;
use JPush\JPushClient;
use JPush\JPushLog;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

use JPush\Exception\APIConnectionException;
use JPush\Exception\APIRequestException;

class Jpush {
    var $br = '<br/>';
    var $spilt = ' - ';

    /**
     * 初始化极光推送参数
     *
     */
    public function __construct() {
      $app_key = CLIENT_JPUSH_APPKEY;
      $master_secret = CLIENT_JPUSH_MASTERSSCRET;
      $this->client = new JPushClient($app_key, $master_secret);

      $app_key = CLIENT_JPUSH_APPKEY;
      $master_secret = CLIENT_JPUSH_MASTERSSCRET;
      $this->doctor = new JPushClient($app_key, $master_secret);

      JPushLog::setLogHandlers(array(new StreamHandler(LOG_PATH.'jpush/'.date('Ymd').'.log', Logger::DEBUG)));
    }

    /**
     * @param $user_tag 用户id，也可是数组
     * @param $content  内容
     * @param string $content_type 内容类型
     * @param array $extras 额外参数
     * @param bool|false $silence
     * @return int
     */
    public function push($user_tag, $content, $content_type='text', $extras=array(), $silence=false) {
        try {
            if($user_tag) {
                $result = $this->client->push()
                ->setPlatform(M\Platform('all'))
                ->setAudience(M\audience(M\alias(is_array($user_tag)?$user_tag:array($user_tag))))
                ->setOptions(M\options(null, null, null, true, null));
            } else {
                logfile('设备别名', 'JPush_APIRequestException_');
                return 0;
            }
            if ($silence) {
                //$result = $result->setMessage(M\message($content, '新消息', $content_type, $extras));
                $result = $result ->setNotification(M\notification($content,
                M\android($content, '新消息', 1, $extras),
                M\ios($content, '新消息', "+1", true, $extras, "")
                ));

            } else {
                $result = $result->setNotification(M\notification($content));
            }
            // ->printJSON()
            // 是否使用定时推送
            if (false) {
                $payload = $result->build();
                $result = $this->client->schedule()->createSingleSchedule("", $payload, array("time"=>date("Y-m-d H:i:s", time()+5)));
            } else {
                $result->send();
            }
            logfile('PUSH:'.$result->getJSON(), 'JPush_');
            return $result;
        } catch (APIRequestException $e) {
            logfile($e, 'JPush_APIRequestException_');
            return 0;
        } catch (APIConnectionException $e) {
            logfile($e, 'JPush_APIConnectionException_');
            return 0;
        }
    }

}
